//引入express模块
const express = require('express');
//引入连接池模块
const pool = require('../pool');
//创建路由器对象
const r = express.Router();

//预约表接口
r.post('/register',(req,res)=>{
    //接收页面传过来的用户id时间  地址
    // console.log(req.body.userid)
    let userid = req.body.userid;
    let time = req.body.time;
    let address = req.body.address;
    //用户预约添加语句
    let sql = 'INSERT register VALUES("",?,?,?)'
    pool.query(sql,[time,address,userid],(error,result)=>{
      //发生错误时执行并抛出
      if(error) throw error;
      // console.log(result)
      if(result.affectedRows){
        //添加成功返回状态码200
        res.send({ message: 'ok', code: 200 });
      }else{
        //添加失败放回状态码500
        res.send({message:'error',code:'500'})
      }
    })
})

//预防和接种api
r.get('/getdetails', (req, res) => {

  // SQL查询
  let sql = "select * from details";
  // 执行SQL查询
  pool.query(sql,(error, results) => {
    if (error) throw error;
    // 返回数据到客户端200
    if(results.length>0){
      res.send({ message: 'ok', code: 200, result: results });
    }else{
      //查询失败返回500！
      res.send({message:'err',code:'500'})
    }
    // console.log(results)
  });
});


//健康自评表
r.get('/assessment',(req,res)=>{
  //查询sql
  let sql = 'select *from selfclass'
  pool.query(sql,(error,result)=>{
    //发生错误时执行并抛出
    if(error) throw error;
    // console.log(result)
    if(result.length){
      //查询成功返回200
      res.send({ message: 'ok', code: 200,results: result });
    }else{
      //查询失败返回500
      res.send({message:'error',code:'500'})
    }
  })
})
//导出路由器对象
//健康自评表更新数据
r.get('/upassessment',(req,res)=>{
    //获取选项id
    var resid = req.query.resultid;
    //获取文章id
    var said = req.query.said;
  //更新sql
  let sql = 'UPDATE self_assessment set result=? where said=?'
  pool.query(sql,[resid,said],(error,result)=>{
    //发生错误时执行并抛出
    if(error) throw error;
    // console.log(result)
    if(result.affectedRows){
      //更新成功返回200
      res.send({ message: 'ok', code: 200 });
    }else{
      //更新失败返回500
      res.send({message:'error',code:'500'})
    }
  })
})


//快速预约模块
r.post('/regit',(req,res)=>{
  //接收页面传过来的用户id时间  地址
  // console.log(req.body.userid)
  let userid = req.body.userid;
  let time = req.body.time;
  let address = req.body.address;
  let idcard = req.body.idcard;
  let phone = req.body.phone;
  let number = req.body.number;
  // console.log(address,time)
  //用户预约添加语句
  let sql = 'SELECT * from regist where regist_id = ?'
  pool.query(sql,[userid],(err,result)=>{
    //查询成功返回201
    if(err) throw err;
    if(result.length){
      res.send({ message: 'ok', code: 201 })
    }else{
      //查询失败
    let sql = 'insert into regist values(null,?,?,?,?,?,?);'
    pool.query(sql,[time,phone,address,number,idcard,userid],(error,result)=>{
    //发生错误时执行并抛出
    if(error) throw error;
    // console.log(result)
    if(result.affectedRows){
      //添加成功返回状态码200
      res.send({ message: 'ok', code: 200 });
    }else{
      //添加失败放回状态码500
      res.send({message:'error',code:500})
    }
  })
    }
  })
  
})
//居民登记表
r.post('/peoplecked',(req,res)=>{
  //接收页面传过来的用户id时间  地址
  // console.log(req.body.userid)
  let userid = req.body.userid;
  let cktime = req.body.cktime;
  let username = req.body.username
  let address = req.body.address;
  let addres = req.body.addres;
  let idcard = req.body.idcard;
  let phone = req.body.phone;
  // console.log(username,cktime)
  // console.log(userid.toString())
  //用户登记添加语句
  let sql = 'select * from peoplem where peop_uid=?'
 
  pool.query(sql,[userid],(err,result)=>{
    if(result.length){
      //已经登记
      res.send({ message: 'ok', code: 500,results: result })
    }
    else{
      //否则添加
      let sql = 'insert into peoplem values(null,?,?,?,?,?,?,?)'
      pool.query(sql,[cktime,phone,address,addres,idcard,username,userid],(error,result)=>{
        //发生错误时执行并抛出
        if(error) throw error;
        // console.log(result)
        if(result.affectedRows){
          //添加成功返回状态码200
          res.send({ message: 'ok', code: 200 });
        }else{
          //添加失败返回状态码500
          res.send({message:'error',code:500})
        }
      })
    }
  })
  
})


//登记查询
r.get('/psearch',(req,res)=>{
  let id = req.query.uid
  let sql = 'select p.peop_name,p.peop_ck_address,p.peop_phone,p.peop_time  from peoplem p where peop_uid=?'
  pool.query(sql,[id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      //查询成功返回200
      res.send({ message: 'ok', code: 200,results: result });
    }else{
      //查询失败返回500
      res.send({message:'error',code:'500'})
    }
  })
})

//用户预约查询表
r.get('/ckregit',(req,res)=>{
  let id = req.query.uid
  let sql = 'SELECT * from  regist'
  pool.query(sql,[id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      //查询成功返回200
      res.send({ message: 'ok', code: 200,results: result });
    }else{
      //查询失败返回500
      res.send({message:'error',code:'500'})
    }
  })
})


//用户是否登录
r.post('/getid',(req,res)=>{
  let id = req.body.idcard
  // console.log(id)
  let sql = 'select peop_card from peoplem where peop_card=?'
  pool.query(sql,[id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      //查询成功返回200
      res.send({ message: 'ok', code: 200,results: result });
    }else{
      //查询失败返回500
      res.send({message:'error',code:'500'})
    }
  })
})


//查询预约记录
//用户预约查询
r.get('/Records',(req,res)=>{
  
  let id = req.query.uid
  // console.log(id)
  let sql = 'SELECT * from  regist where regist_uid=?'
  pool.query(sql,[id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      //查询成功返回200
      res.send({ message: 'ok', code: 200,results: result });
    }else{
      //查询失败返回500
      res.send({message:'error',code:'500'})
    }
  })
})

//查询接种记录状态
//用户预约查询
r.get('/statess',(req,res)=>{

  let id = req.query.uid
  // console.log(id)
  let sql = 'SELECT * from  states where s_id=?'
  pool.query(sql,[id],(err,result)=>{
    if(err) throw err;
    if(result.length){
      //查询成功返回200
      res.send({ message: 'ok', code: 200,results: result });
    }else{
      //查询失败返回500
      res.send({message:'error',code:'500'})
    }
  })
})

//查询问答
r.get('/ansows',(req,res)=>{
  //接收id 
  let tid = req.query.id;
  let sql = 'select* from self_assessment where sa_uid=?'
  //根据id查询问答内容
  pool.query(sql,[tid],(err,result)=>{
    if(err) throw err;
    if(result.length){
      //查询成功返回数据
      res.send({ message: 'ok', code: 200,results: result });
    }else{
      //查询失败
      res.send({message:'error',code:'500'})
    }
  })
})
  /*
  //自我自答提交缺失表
  r.post('/toansows',(req,res)=>{
    //接收id 
    let tid = req.query.id;
    let sql = 'UPDATE self_assessment set back_answer = ? WHERE sa_uid=?'
    //根据id查询问答内容
    pool.query(sql,[tid],(err,result)=>{
      if(err) throw err;
      if(result.length){
        //查询成功返回数据
        res.send({ message: 'ok', code: 200,results: result });
      }else{
        //查询失败
        res.send({message:'error',code:'500'})
      }
    })
  
  
  })
*/


//导出路由器对象
module.exports = r;